package cn.alm.springmvcrestfulcrud.dao;

import cn.alm.springmvcrestfulcrud.entity.Employee;
import jakarta.annotation.Resource;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class EmployeeDaoImpl implements EmployeeDao {

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Override
    public Integer add(Employee employee) {
        String url = "insert into employee(name,age,email,gender,address,salary,birthday) values(?,?,?,?,?,?,?)";
        return jdbcTemplate.update(url, employee.getName(), employee.getAge(), employee.getEmail(), employee.getGender(), employee.getAddress(), employee.getSalary(), employee.getBirthday());
    }

    @Override
    public Integer delete(Integer id) {
        String url = "delete from employee where id = ?";
        int update = jdbcTemplate.update(url, id);
        return update;
    }

    @Override
    public Integer update(Employee employee) {
        String url = "update employee set name = ?,age = ?,email = ?,gender = ?,address = ?,salary = ? where id = ?";
        return jdbcTemplate.update(url, employee.getName(), employee.getAge(), employee.getEmail(), employee.getGender(), employee.getAddress(), employee.getSalary(), employee.getId());
    }

    @Override
    public Employee get(Integer id) {
        String url = "select * from employee where id = ?";
        return jdbcTemplate.queryForObject(url, new BeanPropertyRowMapper<>(Employee.class), id);
    }

    @Override
    public List<Employee> list() {
        String url = "select * from employee";
        return jdbcTemplate.query(url, new BeanPropertyRowMapper<>(Employee.class));
    }
}
